<template>
  <div class="param-main">
    <el-form
      ref="tblMain"
      :model="serachForm"
      labelPosition="right"
      inline=""
      label-suffix=":"
      label-width="110px"
    >
      <el-row :span="24">
        <el-col :span="8">
          <el-form-item label="品牌名称">
            <el-input
              v-model="serachForm.name"
              size="small"
              placeholder=""
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="简拼">
            <el-input
              v-model="serachForm.initial"
              size="small"
              placeholder=""
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="initTableData()"
          >查询</el-button
        >
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="detailTblMain(1)">新增</el-button>
        <el-button size="small" type="danger" @click="deleteEvent(1)"
          >删除</el-button
        >
      </div>
    </el-form>
    <div style="height: calc(100% - 160px)">
      <el-table
        ref="tblMain"
        style="margin-top: 10px"
        :data="listData"
        height="100%"
        @selection-change="handleSelectionChange"
        @sort-change="getSortList"
        v-loading.body="loading"
        border
        stripe
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="200px" >
          <template slot-scope="scope">
            <el-button  type="text" @click="detailTblMain(2,scope.row)" >编辑</el-button>
            <el-divider direction="vertical" ></el-divider>
            <el-button  type="text" @click="deleteEvent(scope.row)">删除</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button  type="text" @click="detailTblMain(3,scope.row)">明细</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="品牌名称" align="center" sortable="custom">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="initial"
          label="简拼"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="gatherTime"
          label="采集时间"
          align="center"
        ></el-table-column>
      </el-table>
    </div>
    <!-- v-show="pageInfo.total != 0" -->
    <el-pagination
      style="margin-top: 10px"
      :current-page.sync="pageInfo.page"
      :page-size="pageInfo.size"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <add-brand
      ref="addBrand"
      v-if="visible"
      @ok="saveData"
      @cancle="cancle"
      :title="title"
      :row="rowData"
    ></add-brand>
  </div>
</template>
<script>
import AddBrand from './addBrand'
import {
  addCarjLetter,
  delCarjLetter,
  upCarjLetter,
  getCarjLetterList,
} from '@/api/carLine'

export default {
  name: 'applyMain',
  props: ['pageData'],
  components: {
    AddBrand
  },
  data() {
    return {
      visible: false,
      rowData: {},
      title: '',
      levelList: [],
      loading: false,
      listData: [],
      selection: [],
      serachForm: {
        initial:"",
        name: '',
        order: 'name,asc'
      },
      pageInfo: {
        page: 1,
        size: 10,
        total: 0
      },
      brands: [],
      letterOb: ''
    }
  },
  watch: {
    pageData() {
      this.initTableData()
    }
  },
  created() {
    // this.letterOb = this.$route.query.data ? this.$route.query.data : '' //品牌对象
    // let op = {
    //   name: this.letterOb.name ? this.letterOb.name : '所有品牌',
    //   path: this.letterOb.path ? this.letterOb.path : '/product/brand'
    // }
    // this.levelList.push(op)
  },
  mounted() {
    // this.getBrandsList().then(() => {
      // this.serachForm.letterId = this.letterOb.id ? this.letterOb.id : ''
      this.initTableData()
    // })
  },
  methods: {
    /**
    *  排序查询
    */
    getSortList(column) {
      let arr = [];
      arr.push(column.prop);
      arr.push( column.order.substring(
           0,
           column.order.indexOf("ending")
       ))
      this.serachForm.order = arr.join(',');
      this.initTableData()
    },
    goBack(path){
      this.$router.push(path)
    },
    /**
     * 点击功能按钮
     */
    detailTblMain(type,row) {
      if (type != 1) {
        this.title = type == 2 ? '修改品牌' : '品牌详情'
        this.rowData = JSON.parse(JSON.stringify(row))
      } else {
        this.title = '新增品牌'
        this.rowData = {}
      }
      this.visible = true
    },
    /**
     * 保存
     */
    saveData(val) {
      if (this.title == '新增品牌') {
        addCarjLetter(val).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.initTableData()
            this.cancle()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      } else {
        upCarjLetter(val).then((res) => {
          const { code = undefined, data = {} } = res.data
          if (!code) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.initTableData()
            this.cancle()
          } else {
            this.$message({
              type: 'error',
              message: '操作失败!'
            })
          }
        })
      }
    },
    /**
     * 关闭弹框
     */
    cancle() {
      this.visible = false
    },
    /**
     * 多选
     */
    handleSelectionChange(val) {
      this.selection = val
    },
    /**
     * 重置
     */
    dataReset() {
      this.pageInfo.page = 1
      this.pageInfo.size = 10
      this.serachForm.initial = ''
      this.serachForm.name = ''
      this.serachForm.order = ''
      this.initTableData()
    },
    /**
     * 分页
     */
    handleSizeChange(val) {
      this.pageInfo.size = val
      this.initTableData()
    },
    /**
     * 分页
     */
    handleCurrentChange(val) {
      this.pageInfo.page = val
      this.initTableData()
    },
    /**
     * 获取表格数据
     */
    initTableData() {
      let me = this
      me.selection = []
      let param = {
        page: me.pageInfo.page,
        size: me.pageInfo.size,
        ...me.serachForm
      }
      me.loading = true
      getCarjLetterList(param)
        .then((res) => {
          me.loading = false
          const { code = undefined, data = {} } = res.data
          if (!code) {
            me.listData = data.content
            me.pageInfo.total = data.totalElements
          }else{
            me.$message.error(data)
          }
        })
        .catch(() => {
          me.$message.error('服务器繁忙')
          me.loading = false
        })
    },
    /**
     * 删除
     */
     deleteEvent(row) {
      let ids = []
      if(row == '1'){
         if (this.selection.length < 1) {
          this.$message.warning('请至少选择一条数据')
          return
        }
        ids = this.selection.map(item=>{
          return item.id
        })
      }else{
        ids.push(row.id)
      }
      this.$confirm('此操作将永久删除该品牌, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delCarjLetter(ids).then((res) => {
            const { code = undefined, data = {} } = res.data
            if (!code) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.initTableData()
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
